package com.anbang.website.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

public class HttpRequest {

	/** HTTP GET method */
	public static final String METHOD_GET = "GET";

	/** HTTP POST method */
	public static final String METHOD_POST = "POST";

	/**
	 * 待请求的url
	 */
	private String url = null;

	/**
	 * 默认的请求方式
	 */
	private String method = METHOD_POST;

	private int timeout = 0;

	private int connectionTimeout = 0;

	/**
	 * Post方式请求时组装好的参数值对
	 */
	private NameValuePair[] parameters = null;

	/**
	 * Get方式请求时对应的参数
	 */
	private String queryString = null;


	
	/**
	 * 默认的请求编码方式
	 */
	private String charset = "UTF-8";

	/**
	 * 请求发起方的ip地址
	 */
	private String clientIp;

	private String userId = "0";
	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getJsonParams() {
		return jsonParams;
	}

	public void setJsonParams(String jsonParams) {
		this.jsonParams = jsonParams;
	}

	private String jsonParams;
	/**
	 * 请求返回的方式
	 */
	private HttpResultType resultType = HttpResultType.BYTES;

	public HttpRequest(){
		
	}
	
	public HttpRequest(HttpResultType resultType) {
		super();
		this.resultType = resultType;
	}

	public String getClientIp() {
		return clientIp;
	}

	public void setClientIp(String clientIp) {
		this.clientIp = clientIp;
	}

	public NameValuePair[] getParameters() {
		return parameters;
	}

	public void setParameters(NameValuePair[] parameters) {
		this.parameters = parameters;
	}

	public String getQueryString() {
		return queryString;
	}

	public void setQueryString(String queryString) {
		this.queryString = queryString;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getMethod() {
		return method;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	public int getConnectionTimeout() {
		return connectionTimeout;
	}

	public void setConnectionTimeout(int connectionTimeout) {
		this.connectionTimeout = connectionTimeout;
	}

	public int getTimeout() {
		return timeout;
	}

	public void setTimeout(int timeout) {
		this.timeout = timeout;
	}

	public String getCharset() {
		return charset;
	}

	public void setCharset(String charset) {
		this.charset = charset;
	}

	public HttpResultType getResultType() {
		return resultType;
	}

	public void setResultType(HttpResultType resultType) {
		this.resultType = resultType;
	}

	public List<NameValuePair> getNameValuePairs(){		
		long timestamp = System.currentTimeMillis() / 1000L;
		List<String> pl = new ArrayList<String>();
		pl.add("appkey=" + Constant.getAppKey());
		pl.add("userid=" + userId);
		pl.add("params=" + jsonParams);
		pl.add("timestamp=" + timestamp);
		Collections.sort(pl);
		String plStr = "";
		for (int i = 0; i < pl.size(); i++)
			plStr += pl.get(i) + ",";
		plStr = plStr.substring(0, plStr.length() - 1);
		//System.out.println("plStr=" + plStr);

		String baseStr = Encodes.urlEncode(String.format("%s%s%s%s%s", method.toUpperCase(), Constant.getSignHostUrl(),url, plStr, Constant.getAppSecret())).replace("+", "%20");
		//System.out.println("baseStr=" + baseStr);
		
		String sign = org.apache.commons.codec.digest.DigestUtils.md5Hex(baseStr);
		//System.out.println("sign=" + sign);
		List<NameValuePair> nvps =  new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("appkey", Constant.getAppKey()));
		nvps.add(new BasicNameValuePair("userid", userId));
		nvps.add(new BasicNameValuePair("params", jsonParams));
		nvps.add(new BasicNameValuePair("timestamp", new Long(timestamp).toString()));
		nvps.add(new BasicNameValuePair("sign", sign));
		
		return nvps;
	}
	
}
